package com.jian.java.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.jian.java.servlet.MyServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * @author Jiange
 * @create 2021-04-29-19:42
 */
@Deprecated
//@Configuration
public class DruildConfig {
    @ConfigurationProperties("spring.datasource")
    @Bean
    public DataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setFilters("stat,wall");//开启数据库的监控功能  防火墙
        return druidDataSource;
    }

    /**
     * 配置Druid的监控页
     * 创建servlet映射 将statViewServlet作为servlet类
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> myServletServletRegistrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
        myServletServletRegistrationBean.addInitParameter("loginUsername","root");
        myServletServletRegistrationBean.addInitParameter("loginPassword","123456");
        return myServletServletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        WebStatFilter webStatFilter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
        webStatFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        webStatFilterFilterRegistrationBean.addInitParameter("exclusions","*.js,*.css,/druid/*");
        return webStatFilterFilterRegistrationBean;
    }
}
